<template>
  <el-dialog :visible.sync="visible" class="mod-config" width="80%" top="8vh">
    <basic-container>
      <div class="boxsty zhiBiao">
          <el-table :data="xpList" v-loading="dataListLoading"  style="max-height: 55vh; overflow-y: auto" stripe tooltip-effect="light" width="100%">
            <el-table-column label="序号" align="center" width="80">
              <template slot-scope="scope">
                {{scope.$index + 1 + page.pageSize*(page.currentPage-1)}}
              </template>
            </el-table-column>
            <el-table-column prop="err" label="错误信息" width="120">
              <template slot-scope="scope">
                <span style="color: red">{{scope.row.err}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="userCode" label="人员编码" width="120">
            </el-table-column>
            <el-table-column prop="userName" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="lx" label="类型" width="120">
            </el-table-column>
            <el-table-column prop="jjxName" label="奖金项名称" width="120">
            </el-table-column>
            <el-table-column prop="jjJe" label="奖金金额" width="120">
            </el-table-column>
            <el-table-column prop="jjDate" :formatter="formater" label="奖惩时间" width="120">
            </el-table-column>
            <el-table-column prop="jjFfzt" label="发放状态" width="120">
            </el-table-column>
            <el-table-column prop="jjFz" label="奖惩分值" width="120">
            </el-table-column>
            <el-table-column prop="jjSm" label="奖惩说明" width="220">
            </el-table-column>
            <el-table-column label="操作"  width="200">
              <template slot-scope="scope">
                <el-button v-if="permissions.jiangjin_jiangjinryllb_edit" type="primary" plain
                  @click="addOrUpdateHandle(scope.row,scope.$index)">修改</el-button>
                <el-button v-if="permissions.jiangjin_jiangjinryllb_del" type="danger" plain
                  @click="deleteHandle(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        <span style="float: right;margin-top: 27px;">
          <el-button :disabled="isErrList.length>0" @click="allImport()" type="primary">全部导入</el-button>
        </span>
<!--        <div style="margin-top:20px">-->
<!--          <el-pagination background layout="total,prev, pager, next,jumper" @current-change="handleCurrentChange"-->
<!--            :current-page="page.currentPage" :page-size="page.pageSize" :total="page.total">-->
<!--          </el-pagination>-->
<!--        </div>-->
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="callockGetList"></table-form>
    </basic-container>
  </el-dialog>
</template>

<script>
  import TableForm from './dataForm'
  import store from '@/store'
  import request from '@/router/axios'
  import { bath } from '@/api/jiangjin/jiangjingrjcjlb'
  import { mapGetters  } from 'vuex'
  import {getJJRyxxbList } from '@/api/jiangjin/jiangjinryxxb';
  import {getJjDeptJjx} from '@/api/jiangjin/jiangjinjjxxxb'


  export default {
    data() {
      return {
        deptId:'',
        visible: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        removeIndex: '',
        xpList: [],
        loading: '',
        errCount: 0,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10, // 每页显示多少条,
          isAsc: false // 是否倒序
        },
        tableCompare: [],
        jjxOptions: [],
        isErrList:[]
      }
    },
    components: {
      TableForm
    },

    computed: {
      ...mapGetters(['permissions'])
    },

    methods: {
      setColumn(sObj,data){
        this.$set(sObj,"userCode",data.userCode)
        this.$set(sObj,"userName",data.userName)
        this.$set(sObj,"jjxId",data.jjxId)
        this.$set(sObj,"jjxName",data.jjxName)

        if(data.jjJe < 0){
          this.$set(sObj,"jjJe",Math.abs(data.jjJe))
          this.$set(sObj,"lx","惩罚")
        }else {
          this.$set(sObj,"jjJe",data.jjJe)
          this.$set(sObj,"lx","奖励")
        }

        this.$set(sObj,"jjDate",data.jjDate)
        this.$set(sObj,"jjFz",data.jjFz)
        this.$set(sObj,"jjSm",data.jjSm)
        this.$set(sObj,"err","")
      },

      callockGetList(index, data) {
        // this.xpList.splice(index, 1);
        // this.xpList.push(data);

        // let dIndex = 0;
        // dIndex = index + 1 + this.page.pageSize*(this.page.currentPage-1)

        // this.setColumn(this.xpList[index],data);
        // this.setColumn(this.dataList[dIndex -1],data);

        //console.log(data);

        this.$set(this.xpList[index],"userCode",data.userCode)
        this.$set(this.xpList[index],"userName",data.userName)
        this.$set(this.xpList[index],"jjxId",data.jjxId)
        this.$set(this.xpList[index],"jjxName",data.jjxName)

        if(data.jjJe < 0){
          this.$set(this.xpList[index],"jjJe",Math.abs(data.jjJe))
          this.$set(this.xpList[index],"lx","惩罚")
        }else {
          this.$set(this.xpList[index],"jjJe",data.jjJe)
          this.$set(this.xpList[index],"lx","奖励")
        }

        this.$set(this.xpList[index],"jjDate",data.jjDate)
        this.$set(this.xpList[index],"jjFz",data.jjFz)
        this.$set(this.xpList[index],"jjSm",data.jjSm)
        this.$set(this.xpList[index],"err",data.err)
        //this.$set(this.xpList[index],"errNew","")
        this.isErrList=[]
        this.xpList.map(item=>{
          if(item.err){
           this.isErrList.push(item.err)
          }
        })
        this.$forceUpdate();

      },
      //根据人员编号取得人员姓名
      getRyXm(ryCode){
        let lResult = "";

        this.tableCompare.find(u =>{
          if(u.ryCode === ryCode){
            lResult = u.ryXm;
          }
        })

        return lResult;
      },
      //根据奖金项名称取得id
      getJjxId(ryMc){
        let lResult = "";
        this.jjxOptions.find(u =>{
          if(u.jjxMc.toString().trim() === ryMc.toString().trim()){
            lResult = u.id;
          }
        })
        return lResult;
      },
      getErr(lResult){
        let bErr = false;
        let sRyXm = "";
        let sJjxId = "";

        for(let i=0;i<lResult.length;i++){

          this.$set(lResult[i],"errNew","");
          sRyXm = "";
          sJjxId = "";

          if(lResult[i]["userCode"] == null || lResult[i]["userCode"] === ""){
            this.$set(lResult[i],"errNew","人员编码为空");
            continue;
          }else {
            sRyXm =this.getRyXm(lResult[i]["userCode"]);
            if(sRyXm === ""){
              //this.$set(lResult[i],"errNew","系统中查无此人");
              continue;
            }
          }

          if(lResult[i]["userName"] == null || lResult[i]["userName"] === ""){
            this.$set(lResult[i],"errNew","人员姓名为空");
            continue;
          }
          if(lResult[i]["jjxName"] == null || lResult[i]["jjxName"] === ""){
            this.$set(lResult[i],"errNew","奖金项名称为空");
            continue;
          }else {
            sJjxId = this.getJjxId(lResult[i]["jjxName"]);
            if(sJjxId === ""){
              this.$set(lResult[i],"errNew","系统中无此奖金项");
              continue;
            }
          }

          if(lResult[i]["jjJe"] == null || lResult[i]["jjJe"] === ""){
            this.$set(lResult[i],"errNew","奖金金额为空");
            continue;
          }
          if(lResult[i]["jjDate"] == null || lResult[i]["jjDate"] === ""){
            this.$set(lResult[i],"errNew","奖惩时间为空");
            continue;
          }
          if(lResult[i]["jjFz"] == null || lResult[i]["jjFz"] === ""){
            this.$set(lResult[i],"errNew","奖惩分值为空");
            continue;
          }
       }

        for(let i=0;i<lResult.length;i++){
          if(lResult[i]["errNew"] !==""){
            bErr = true;
            break;
          }
        }

        return bErr;
      },
      /**
       * 全部导入数据
       */
      allImport() {
        var _this = this;


        ////console.log(this.xpList);
        ////console.log(this.dataList);

        //if(this.getErr(this.xpList)) return;


        var msg = '';
        // if (_this.errCount > 0) {
        //   msg = '存在' + _this.errCount + '条错误数据确认导入?'
        // } else {
          msg = '确认全部导入?';
        // }
        //
        // return;

        this.$confirm(msg, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          _this.loading = _this.$loading({
            lock: true,
            text: '人员数据导入中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          for (var i = 0; i < _this.xpList.length; i++) {
            if (_this.xpList[i].jjDate) {
              _this.xpList[i].jjDate = _this.$moment(_this.xpList[i].jjDate).format("YYYY-MM-DD")
            }
          }

          return bath(_this.xpList)
        }).then(data => {
          _this.visible = false
          _this.$emit('refreshDataList')
          _this.$message.closeAll()
          _this.$message.success('导入成功')
          _this.loading.close()
        })
      },
      handleCurrentChange(val) {
        if (val === 1) {
          this.xpList = this.dataList.slice(0, this.page.pageSize);
        } else {
          this.xpList = this.dataList.slice(val * this.page.pageSize - this.page.pageSize, val * this.page.pageSize);
        }
        this.page.currentPage = val;
      },
      //分页
      getPagingShow(dataList) {
        this.dataList = dataList;
        this.page.total = dataList.length;
        this.xpList = this.dataList.slice(0, this.page.pageSize);
        this.page.currentPage = 1;
      },
      setValidata(sData){
        let sStr = [];
        let lResult =[];

        sStr =sData;

        for(let i=0;i<sStr.length;i++){
          if(sStr[i]["userCode"] ==="" && sStr[i]["userName"] ==="" &&
            sStr[i]["jjxName"] ==="" && sStr[i]["jjDate"] ==="" &&
            sStr[i]["jjSm"] ===""){
          }else {
            lResult.push(sStr[i]);
          }
        }

        for(let i=0;i<lResult.length;i++){
          if(lResult[i]["userCode"] == null || lResult[i]["userCode"] === ""){
            this.$set(lResult[i],"errNew","人员编码为空");
            continue;
          }
          if(lResult[i]["userName"] == null || lResult[i]["userName"] === ""){
            this.$set(lResult[i],"errNew","人员姓名为空");
            continue;
          }
          if(lResult[i]["jjxName"] == null || lResult[i]["jjxName"] === ""){
            this.$set(lResult[i],"errNew","奖金项名称为空");
            continue;
          }
          if(lResult[i]["jjJe"] == null || lResult[i]["jjJe"] === ""){
            this.$set(lResult[i],"errNew","奖金金额为空");
            continue;
          }
          if(lResult[i]["jjDate"] == null || lResult[i]["jjDate"] === ""){
            this.$set(lResult[i],"errNew","奖惩时间为空");
            continue;
          }
          if(lResult[i]["jjFz"] == null || lResult[i]["jjFz"] === ""){
            this.$set(lResult[i],"errNew","奖惩分值为空");
            continue;
          }

          // if(lResult[i]["jjSm"] == null || lResult[i]["jjSm"] === ""){
          //   this.$set(lResult[i],"errNew","奖惩说明为空");
          //   continue;
          // }

        }


        return lResult;
      },
      // 获取数据列表
      getDataList(res,deptId) {

        this.visible = true;
        this.dataListLoading = true
        this.deptId = deptId;

        this.tableCompare = [];
        getJJRyxxbList().then(res =>{
          this.tableCompare = res.data.data;
          ////console.log(this.tableCompare);
        })

        this.jjxOptions = [];
        getJjDeptJjx(deptId).then(res => {
          this.jjxOptions = res.data.data;
           ////console.log(this.jjxOptions)
        })


         this.dataList = res.data.data;
        //this.dataList = this.setValidata(res.data.data);

        this.xpList = this.dataList;
        this.xpList.map(item=>{
          if(item.err){
           this.isErrList.push(item.err)
          }
        })
      
        // this.errCount = res.data.msg;
        // this.getPagingShow(this.dataList)
        this.dataListLoading = false




        ////console.log(this.xpList);
      },
      // 新增 / 修改
      addOrUpdateHandle(r, index) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(r, index,this.deptId)
        })
      },
      // 删除
      deleteHandle(index) {
        var _this = this;
        this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          _this.xpList.splice(index, 1)
          _this.isErrList=[]
          _this.xpList.map(item=>{
          if(item.err){
           _this.isErrList.push(item.err)
          }
        })
        console.log(_this.isErrList.length)
        }).then(data => {
          this.$message.closeAll()
          this.$message.success('删除成功')

        })
      },
      nShowIndex(row, column, cellValue, index) {
        return index + 1;
      },
      formater(row, column, cellValue, index) {
        if (cellValue) {
          return this.$moment(cellValue).format("YYYY-MM-DD");
        } else {
          return cellValue;
        }
      }
    }
  }

</script>
